package br.hm.operandroid.activity;

import java.util.Collections;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import br.hm.operandroid.R;

public class GenericAdapter<T> extends BaseAdapter {

	@SuppressWarnings("unchecked")
	public List<T> list = Collections.EMPTY_LIST;

	private LayoutInflater layoutInflater;

	private GenericFormatter<T> imageFormatter;

	private GenericFormatter<T> sampleLeftLabelFormatter;

	private GenericFormatter<T> sampleRightFormatter;

	private GenericFormatter<T> fullLabelFormatter;

	public GenericAdapter(Context context, GenericFormatter<T> imageFormatter,
			GenericFormatter<T> sampleLeftLabelFormatter,
			GenericFormatter<T> fullLabelFormatter,
			GenericFormatter<T> sampleRightFormatter) {

		this.imageFormatter = imageFormatter;
		this.sampleLeftLabelFormatter = sampleLeftLabelFormatter;
		this.sampleRightFormatter = sampleRightFormatter;
		this.fullLabelFormatter = fullLabelFormatter;
		this.layoutInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int index) {
		return (Object) list.get(index);
	}

	@Override
	public long getItemId(int index) {
		// TODO Auto-generated method stub
		return index;
	}

	@Override
	public View getView(int index, View view, ViewGroup viewGroup) {
		T object = list.get(index);

		View v = layoutInflater.inflate(R.layout.layout_row_operator,
				null);

		imageFormatter.formatter(object, v.findViewById(R.id.historyImage));

		sampleLeftLabelFormatter.formatter(object,
				v.findViewById(R.id.historySimpleLabel));

		fullLabelFormatter.formatter(object,
				v.findViewById(R.id.historyFullLabel));

		sampleRightFormatter.formatter(object,
				v.findViewById(R.id.historyDateLabel));

		return v;
	}

}
